package com.woniu.hospital.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.hospital.domain.Equipemnt;
import com.woniu.hospital.domain.EquipemntVo;
import com.woniu.hospital.domain.EquipmentType;
import com.woniu.hospital.service.IEquipemntService;
import com.woniu.hospital.service.IEquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备库存表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/equipemnt")
public class EquipemntController extends BaseController
{
    @Autowired
    private IEquipemntService equipemntService;
    @Autowired
    private IEquipmentTypeService iEquipmentTypeService;
    /**
     * 查询设备库存表列表
     */
    @RequiresPermissions("hospital:equipemnt:list")
    @GetMapping("/list")
    public TableDataInfo list(Equipemnt equipemnt)
    {
        startPage();
        List<Equipemnt> list = equipemntService.selectEquipemntList(equipemnt);
        return getDataTable(list);
    }

    @RequiresPermissions("hospital:equipemnt:list")
    @GetMapping("/showAll")
    public TableDataInfo showAllYan(EquipemntVo equipemntVo)
    {

        startPage();
        List<EquipemntVo> equipemntVolist = new ArrayList<EquipemntVo>();
        Equipemnt equipemnt = new Equipemnt();
        equipemnt.setEquipmentName(equipemntVo.getEquipmentName());
        equipemnt.setEquipmentNum(equipemntVo.getEquipmentNum());
        equipemnt.setEquipmentPrice(equipemntVo.getEquipmentPrice());
        if(equipemntVo.getEquipmentTypeid() != null ){
            equipemnt.setEquipmentTypeid(equipemntVo.getEquipmentTypeid());
        }
        List<Equipemnt> list = equipemntService.selectEquipemntList(equipemnt);
        for (int i = 0; i < list.size(); i++) {
            EquipemntVo equipemntVo2 = new EquipemntVo();
            equipemntVo2.setEquipmentId(list.get(i).getEquipmentId());
            equipemntVo2.setEquipmentName(list.get(i).getEquipmentName());
            equipemntVo2.setEquipmentPrice(list.get(i).getEquipmentPrice());
            equipemntVo2.setEquipmentNum(list.get(i).getEquipmentNum());
            equipemntVo2.setEquipmentTypeid(list.get(i).getEquipmentTypeid());
            equipemntVo2.setEquipmentTypeName(iEquipmentTypeService.selectEquipmentTypeByEquipmentTypeid(
                    list.get(i).getEquipmentTypeid()
            ).getEquipmentTypename());
            equipemntVolist.add(equipemntVo2);
        }
        return getDataTable(equipemntVolist);
    }
    /**
     * 导出设备库存表列表
     *
    @RequiresPermissions("hospital:equipemnt:export")
    @Log(title = "设备库存表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipemnt equipemnt)
    {
        List<Equipemnt> list = equipemntService.selectEquipemntList(equipemnt);
        ExcelUtil<Equipemnt> util = new ExcelUtil<Equipemnt>(Equipemnt.class);
        util.exportExcel(response, list, "设备库存表数据");
    }




    /**
     * 获取设备库存表详细信息
     */
    @RequiresPermissions("hospital:equipemnt:query")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(equipemntService.selectEquipemntByEquipmentId(equipmentId));
    }

    /**
     * 获取设备类型表详细信息
     */
    @RequiresPermissions("hospital:equipemnt:query")
    @GetMapping("/getEquipemntTypeYan")
    public AjaxResult getEquipemntTypeYan() {
        return success(iEquipmentTypeService.selectEquipmentTypeList(null));
    }

    @RequiresPermissions("hospital:equipemnt:query")
    @GetMapping("/yan/{equipmentId}")
    public AjaxResult getEquipemntByIdYannfo(@PathVariable("equipmentId") String equipmentId)
    {
        EquipemntVo equipemntVo = new EquipemntVo();
        Equipemnt equipemnt = equipemntService.selectEquipemntByEquipmentId(Long.valueOf(equipmentId));
        equipemntVo.setEquipmentId(equipemnt.getEquipmentId());
        equipemntVo.setEquipmentName(equipemnt.getEquipmentName());
        equipemntVo.setEquipmentPrice(equipemnt.getEquipmentPrice());
        equipemntVo.setEquipmentNum(equipemnt.getEquipmentNum());
        equipemntVo.setEquipmentTypeid(equipemnt.getEquipmentTypeid());
        equipemntVo.setEquipmentTypeName(iEquipmentTypeService.selectEquipmentTypeByEquipmentTypeid(equipemnt.getEquipmentTypeid()).getEquipmentTypename());
        return success(equipemntVo);
    }




    /**
     * 新增设备库存表
     */
    @RequiresPermissions("hospital:equipemnt:add")
    @Log(title = "设备库存表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipemnt equipemnt)
    {
        return toAjax(equipemntService.insertEquipemnt(equipemnt));
    }


    @RequiresPermissions("hospital:equipemnt:add")
    @Log(title = "设备库存表", businessType = BusinessType.INSERT)
    @PostMapping("/addEquipemntYan")
    public AjaxResult addEquipemntYan(@RequestBody EquipemntVo equipemntVo)
    {
        Equipemnt equipemnt = new Equipemnt();
        equipemnt.setEquipmentName(equipemntVo.getEquipmentName());
        equipemnt.setEquipmentNum(equipemntVo.getEquipmentNum());
        equipemnt.setEquipmentPrice(equipemntVo.getEquipmentPrice());
        equipemnt.setEquipmentTypeid(equipemntVo.getEquipmentTypeid());
        return toAjax(equipemntService.insertEquipemnt(equipemnt));
    }
    /**
     * 修改设备库存表
     */
    @RequiresPermissions("hospital:equipemnt:edit")
    @Log(title = "设备库存表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipemnt equipemnt)
    {
        return toAjax(equipemntService.updateEquipemnt(equipemnt));
    }

    @RequiresPermissions("hospital:equipemnt:edit")
    @Log(title = "设备库存表", businessType = BusinessType.UPDATE)
    @PutMapping("/updateEquipemntYan")
    public AjaxResult updateEquipemntYan(@RequestBody EquipemntVo equipemntVo)
    {
        Equipemnt equipemnt = new Equipemnt();
        equipemnt.setEquipmentId(equipemntVo.getEquipmentId());
        equipemnt.setEquipmentNum(equipemntVo.getEquipmentNum());
        return toAjax(equipemntService.updateEquipemnt(equipemnt));
    }


    /**
     * 删除设备库存表
     */
    @RequiresPermissions("hospital:equipemnt:remove")
    @Log(title = "设备库存表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipemntService.deleteEquipemntByEquipmentIds(equipmentIds));
    }
}
